// pages/kanjia/scan_result/index.js
var app = getApp(),
  a = app.requirejs("core"),
  QR = app.requirejs("qrcode");
Page({

  /**
   * 页面的初始数据
   */
  data: {
    imagePath: 'http://ddfwz.sssvip.net/asmce/kanjia/timg.gif'
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (e) {
    var i = getApp().getCache("userinfo").openid;
    if(i){
      this.setData({
        openid:i
      })
      this.getInfo();
    }else{
      a.alert('未获取到用户信息');
    }
    
  },
  getInfo: function () {
    var size = this.setCanvasSize();//动态设置画布大小
    var timestamp = Date.parse(new Date())/1000;
    var data = { 'id': this.data.openid,'add_staff':520 ,'over_time': timestamp+30*60 };
    var initUrl = escape(JSON.stringify(data));
    // var bb = unescape(aa);
    // var cc = JSON.parse(bb);
    this.createQrCode(initUrl, "mycanvas", size.w, size.h);
  },
  //适配不同屏幕大小的canvas
  setCanvasSize: function () {
    var size = {};
    try {
      var res = wx.getSystemInfoSync();
      var scale = 750 / 686;//不同屏幕下canvas的适配比例；设计稿是750宽
      var width = res.windowWidth / scale;
      var height = width;//canvas画布为正方形
      size.w = width;
      size.h = height;
    } catch (e) {
      // Do something when catch error
      console.log("获取设备信息失败" + e);
    }
    return size;
  },
  createQrCode: function (url, canvasId, cavW, cavH) {
    //调用插件中的draw方法，绘制二维码图片
    QR.api.draw(url, canvasId, cavW, cavH);
    setTimeout(() => { this.canvasToTempImage(); }, 1000);

  },
  //获取临时缓存照片路径，存入data中
  canvasToTempImage: function () {
    var that = this;
    wx.canvasToTempFilePath({
      canvasId: 'mycanvas',
      success: function (res) {
        console.log(res)
        var tempFilePath = res.tempFilePath;
        console.log(tempFilePath);
        that.setData({
          imagePath: tempFilePath,
           canvasHidden:true
        });
      },
      fail: function (res) {
        console.log(res);
      }
    });
  },
  //点击图片进行预览，长按保存分享图片
  previewImg: function (e) {
    var img = this.data.imagePath;
    wx.previewImage({
      current: img, // 当前显示图片的http链接
      urls: [img] // 需要预览的图片http链接列表
    })
  },
  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    var i = app.getCache('userinfo').openid;
    this.setData({
      imagePath: 'http://ddfwz.sssvip.net/asmce/kanjia/timg.gif'
    })
    if (i) {
      this.getInfo();
    }
    wx.stopPullDownRefresh();
  },
  
  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})